import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as paymentMethodBaseApi from "@/api/payment-method-base";

export const usePaymentMethodBaseStore = defineStore(
  "paymentMethodBase",
  () => {
    async function getPaymentMethods(param?: any) {
      const response = await paymentMethodBaseApi.getPaymentMethods(param);
      if (response.code === 20000) {
        return response.data.paymentMethods;
      }
      return [];
    }

    async function getPaymentMethod(param?: any) {
      const response = await paymentMethodBaseApi.getPaymentMethod(param);
      if (response.code === 20000) {
        return response.data.paymentMethod;
      }
      return {};
    }

    async function savePaymentMethod(param?: any) {
      const apiCall = param.id
        ? paymentMethodBaseApi.updatePaymentMethod
        : paymentMethodBaseApi.createPaymentMethod;
      const response = await apiCall({ ...param });
      if (response.code === 20000) {
        Message.success(param.id ? "更新成功" : "新增成功");
      }
    }

    async function deletePaymentMethod(param?: any) {
      const response = await paymentMethodBaseApi.deletePaymentMethod(param);
      if (response.code === 20000) {
        Message.success("移除成功");
      }
    }

    return {
      getPaymentMethods,
      getPaymentMethod,
      savePaymentMethod,
      deletePaymentMethod,
    };
  },
);
